package br.com.loader.models;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;

import br.com.loader.model.ObjModel;
import br.com.loader.texture.Texture;

public class Model {
	/** The texture to apply to the model */
	private Texture texture;
	/** The model to be displayed for the player */
	private ObjModel model;
	/** The X component of the forward vector - used to place shots */
	private float forwardX = 0;
	/** The Y component of the forward vector - used to place shots */
	private float forwardY = 1;
	int rotationZ;
	int rotationX;
	int rotationY;

	/**
	 * Cria um novo modelo
	 * 
	 * @param texture
	 *            The texture to apply to the player's model
	 * @param model
	 *            The model to display for the player
	 * @param shotTexture
	 *            The texture to apply to the shot's created when the player
	 *            fires
	 */
	public Model(Texture texture, ObjModel model) {
		this.texture = texture;
		this.model = model;
	}

	/**
	 * @see org.newdawn.asteroids.entity.Entity#update(org.newdawn.asteroids.entity.EntityManager,
	 *      int)
	 */
	public void update(int delta) {
		// if the player is pushing left or right then rotate the
		// model. Note that the amount rotated is scaled by delta, the
		// amount of time that has passed. This means that rotation
		// stays framerate independent
		if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
			rotationX += (delta / 5.0f);
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
			rotationX -= (delta / 5.0f);
		}
		//if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
			rotationZ += (delta / 5.0f);
		//}
		if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
			rotationZ -= (delta / 5.0f);
		}
	}

	/**
	 * @see org.newdawn.asteroids.entity.Entity#render()
	 */
	public void render() {
		// enable lighting for the player's model
		GL11.glEnable(GL11.GL_LIGHTING);

		// store the original matrix setup so we can modify it
		// without worrying about effecting things outside of this
		// class
		GL11.glPushMatrix();

		// position the model based on the players currently game
		// location
		GL11.glTranslatef(0, 0, 0);

		// rotate the ship round to our current orientation for shooting
		//GL11.glRotatef(rotationZ, 0, 0, 1);
		//GL11.glRotatef(rotationX, 1, 0, 0);
		GL11.glRotatef(rotationZ, 0, 1, 0);
		GL11.glRotatef(rotationX, 1, 0, 0);

		// setup the matrix to draw the model for our player
		// rotate the ship to the right orientation for rendering. Our
		// ship model is modelled on a different axis to the one we're
		// using so we'd like to rotate it round
		GL11.glRotatef(90, 0, 0, 1);
		GL11.glRotatef(90, 0, 1, 0);
		GL11.glRotatef(270, 0, 0, 1);

		// scale the model down because its way to big by default
		GL11.glScalef(0.05f, 0.05f, 0.05f);

		// bind to the texture for our model then render it. This
		// actually draws the geometry to the screen
		texture.bind();
		model.render();

		// restore the model matrix so we leave this method
		// in the same state we entered
		GL11.glPopMatrix();
	}
}
